package com.sita.linboard.driverInfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sita.linboard.MainMessage.DriverDayMsgRequest;
import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.Constants;
import com.sita.linboard.utils.LogUtils;
import com.sita.linboard.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/2.
 */
public class MsgCenterActivity extends BaseActivity implements View.OnClickListener{
    private TextView title,clearAll;
    private LinearLayout back;
    private ListView msgList;
    private MsgListAdapter dataAdapter;
    private int page=0;
    private int size=10;
    @Override
    protected void initData() {
        title.setText("消息中心");
        getMsgFromSer(page,size);
    }
    private void getMsgFromSer(int page,int size){
        MsgCenterRequest request = new MsgCenterRequest();
        request.page = page;
        request.size = size;
        request.user_id = SpUtils.getString("AccoundID",null, BaseApplication.getContext());

        RestClient.getRestService().MsgCenter(request, new Callback<MsgCenterBackBean>() {
            @Override
            public void success(MsgCenterBackBean msgCenterBackBean, Response response) {
                if (msgCenterBackBean.getErrorCode().equals("0")&&msgCenterBackBean.getData()!=null){
                          dataAdapter.setData(msgCenterBackBean.getData(),false);
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        title = bindView(R.id.head_title);
        back = bindView(R.id.img_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        clearAll = bindView(R.id.clear_all_text);
        clearAll.setOnClickListener(this);
        clearAll.setVisibility(View.VISIBLE);
        msgList = bindView(R.id.msg_center_list);
        dataAdapter = new MsgListAdapter(this);
        msgList.setAdapter(dataAdapter);
        View view = LayoutInflater.from(this).inflate(R.layout.listivew_foot,null);
        msgList.addFooterView(view);
    }

    @Override
    protected int getId() {
        return R.layout.activity_msgcenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.clear_all_text:
                //清空所有的数据
               ClearAllData();
                break;
        }
    }
    private void ClearAllData(){
        DriverDayMsgRequest request = new DriverDayMsgRequest();
        request.user_id = SpUtils.getString("AccoundID",null, BaseApplication.getContext());
        RestClient.getRestService().ClearAllMsg(request, new Callback<ClearBackBean>() {
            @Override
            public void success(ClearBackBean clearBackBean, Response response) {
                if (clearBackBean.getErrorCode().equals("0")&&clearBackBean.getData()==null){
                    LogUtils.e("MsgCenterActivity","清空成功");
                    dataAdapter.setData(null,true);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                LogUtils.e("MsgCenterActivity","清空失败");
            }
        });
    }
    public static void MsgCenterIntent(Context context){
        Intent intent = new Intent(context,MsgCenterActivity.class);
        context.startActivity(intent);
    }
}
